export default {
    data: {
        result:0,
        conW : 0,// 游戏界面宽
        conH : 0,
        snakeBody: 20,
        h:0,
        w:0,
        grid:[],
        food:{
            x:0,
            y:0
        },
        timeId:null, // 计时器id
        level:5,//档位
        des:{//蛇的方向
          "-20":{ // 向上移动一位
              x:0,
              y:-1,
              flag: ''
          },
          "20":{//向下
              x:0,
              y:1,
              flag: ''
          },
          "10":{ //右
              x:1,
              y:0,
              flag: ''
          },
          "-10":{ //左
              x:-1,
              y:0,
              flag: ''
          }
        },
        isStart:false,
        snake:{
            snakePos:null
        },
        currDes:{x:1,y:0},
        isEndP:false

    },
    init(){
        const snakePos = [
            {
                x : 0,
                y : 0,
                flag : 'b',
                id : Math.random()
            },
            {
                x : 1,
                y : 0,
                flag : 'b',
                id : Math.random()
            },
            {
                x : 2,
                y : 0,
                flag : 'h',
                id : Math.random()
            }
        ];
        this.snake.snakePos = snakePos
        this.randomFood()

    },
    onShow(){ // 第一次初始化
        this.conH = this.$refs["con"].getBoundingClientRect().height ;
        this.conW = this.$refs["con"].getBoundingClientRect().width ;
        this.h = Math.floor(this.conH / this.snakeBody);
        this.w = Math.floor(this.conW / this.snakeBody);
        for (var i = 0; i < this.w; i++) {
            this.grid.push([])
            for (var j = 0; j < this.h; j++) {
                this.grid[i].push({
                    x: i,
                    y: j
                });
            }
        }
        this.init();

    },
    isEnd(newHead){
        if(newHead.x >= 18 || newHead.x < 0 || newHead.y >= 27 || newHead.y < 0){
            this.setIsEnd();
        }

        let is = this.snake.snakePos.find((item)=>{
            return item.x == newHead.x && item.y == newHead.y
        })
        if(is){
            this.setIsEnd();
        }




    },
    setIsEnd(){
        clearInterval(this.timeId)
        this.isEndP = true
    }
    ,
    randomFood(){ //随机生成食物
        while(true){
            let x = Math.floor(Math.random() * this.conW);
            let y = Math.floor(Math.random() * this.conH);
            x = x - (x % this.snakeBody);
            y = y - (y % this.snakeBody);
            let is = this.snake.snakePos.find((item)=>{
                return item.x == x && item.y == y
            })
            this.food.x = x;
            this.food.y = y;
            if(!is) {
                break;
            }
        }
    },
    addHead(des){
        const oHead = this.snake.snakePos[this.snake.snakePos.length -1];
        const newHead ={
            x : oHead.x + des.x,
            y : oHead.y + des.y,
            flag : 'h',
            id : Math.random()
        }
        this.isEnd(newHead);
        this.snake.snakePos.push(newHead);

        oHead.flag = 'b';
    },
    move(des){ // 蛇移动时，原头变身体，原尾巴去掉，也就是push一个头，shift一个尾巴
        this.addHead(des);
        this.snake.snakePos.shift();

    },
    intervalMove(d){ // 自动跑
        if(!this.isStart) return
        clearInterval(this.timeId);
        this.timeId = setInterval(()=>{
            const head = this.snake.snakePos[this.snake.snakePos.length - 1];
            this.move(d);
            if(this.snakeBody * head.x == this.food.x && this.food.y == this.snakeBody * head.y ){
               this.addHead(d);
               this.randomFood();
                this.result++;
            }
        },1000/this.level);
    },
    isCuurDes(value = '',x1,x2){
        // 判断当前蛇的方向,x1 为新方向，x2为以前的方向，主要是判断点击的按钮是否左右，上下冲突
        if((+x1 + + x2) == 0 ) return false;
        if(this.isEndP) return ;
        this.currDes = value; //存下方向

        return true;
    },
    clickBut(m){// 点击按钮
        const value = m.target.dataSet.value;
        switch(value){
            case "-20":{ //上
              //判断方向是否相反，如果相反则不切换方向
              this.isCuurDes(this.des[value],this.des[value].y,this.currDes.y)
                && this.intervalMove(this.des[value]);
              break;
            }
            case "20":{// 下
                this.isCuurDes(this.des[value],this.des[value].y,this.currDes.y)
                  && this.intervalMove(this.des[value]);
                break;
            }
            case "-10":{ //左
                this.isCuurDes(this.des[value],this.des[value].x,this.currDes.x)
                  && this.intervalMove(this.des[value]);
                break;
            }
            case "10":{ // 右
                this.isCuurDes(this.des[value],this.des[value].x,this.currDes.x)
                  && this.intervalMove(this.des[value]);
                break;
            }
            case "1": { //开始或暂停
                if(this.isEndP) return
                this.isStart = !this.isStart;
                if(this.isStart && !this.isEndP){
                    this.intervalMove(this.currDes);
                }else{
                    clearInterval(this.timeId);
                }
                break;
            }
        }
    },
    reInit(){
        this.init();
        this.result = 0;
        this.isEndP = false;
        this.isStart = false;
        this.currDes={x:1,y:0};
    }


}
